<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" v-model="inputValue" />
    <a-button type="primary" @click="addTask">添加事项</a-button>

    <a-list bordered :dataSource="toggleList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox v-model="item.done">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click='delTask(item.id)' >删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDone}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="toggle('all')" :type="key == 'all'? 'primary':'default' " >全部</a-button>
          <a-button @click="toggle('unDone')" :type="key == 'unDone'? 'primary':'default' ">未完成</a-button>
          <a-button @click="toggle('done')" :type="key == 'done'? 'primary':'default' ">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click.prevent="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
      inputValue: '',
      nextId: 5

    }
  },
  created () {
    this.$store.dispatch('getList')
  },
  // 计算属性的值不能被修改
  computed: {
    ...mapGetters(['unDone', 'toggleList']),

    ...mapState(['list', 'key'])
  },
  methods: {
    ...mapMutations(['add', 'clearDone']),
    addTask () {
      if (this.inputValue.trim()) {
        this.nextId++
        var option = {
          id: this.nextId,
          info: this.inputValue,
          done: false
        }
        this.add(option)
      }
    },
    delTask (id) {
      this.$store.commit('reduce', id)
    }

  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
